package demo3;

import java.util.Stack;

class MyQueue {
    public Stack<Integer> stack1;
    public Stack<Integer> stack2;
    public MyQueue() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }
    
    //放的时候都放在stack1中
    public void push(int x) {
        stack1.push(x);
    }
    
    //出的时候都在stack2出
    public int pop() {
    //先判断stack1 和 stack2 是否为空
    if (empty()) {
        return -1;
    }
    //判断stack2是否为空
    //不为空就弹出stack2栈顶的值
    int val = 0;
    if (!stack2.empty()) {
        val = stack2.pop();
    } else {
        //如果为空的话  就把stack1中的值全部倒入stack2中
        while (!stack1.empty()) {
            val = stack1.pop();
            stack2.push(val);
        }
        //弹出stack2栈顶的元素
        val= stack2.pop();
    }
    return val;
    }
    
    public int peek() {
        //判断stack1 和 stack2 是否为空
        if (empty()) return -1;
        //判断stack2 是否为空
        int val = 0;
        if (!stack2.empty()) {
            val = stack2.peek();
        } else {
            //如果为空的话
            //把stack1 中的元素倒给stack2
            while (!stack1.empty()) {
                val = stack1.pop();
                stack2.push(val);
            }
        }
        return val;
    }
    
    public boolean empty() {
        if (stack1.empty() && stack2.empty()) {
            return true;
        }
        return false;
    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */